package com.github.murzagalin.evaluator.function

abstract class OneNumberArgumentFunction(name: String, argsCount: IntRange) : Function(name, argsCount) {

    constructor(name: String, argsCount: Int): this(name, argsCount..argsCount)

    constructor(name: String, minArgsCount: Int, maxArgsCount: Int): this(name, minArgsCount..maxArgsCount)

    override fun invoke(vararg args: Any): Any {
        require(args.size == 1) { "$name function requires 1 argument" }
        val operand = args.getAsNumber(0) {
            "$name is called with argument type ${Number::class.simpleName}, but supports only numbers"
        }

        return invokeInternal(operand)
    }

    abstract fun invokeInternal(arg: Number): Double
}